package com.example.memo1.adapter;

import android.text.TextUtils;
import android.widget.Filter;

import com.example.memo1.bean.Record;

import java.util.ArrayList;
import java.util.List;

public class RecordFilter extends Filter {
    private List<Record> mRecordList;
    private RecordAdapter adapter;

    @Override
    protected FilterResults performFiltering(CharSequence charSequence) {
        FilterResults filterResults = new FilterResults();
        if (TextUtils.isEmpty(charSequence)) {
            // 如果搜索关键字为空，则返回全部备忘录记录
            filterResults.count = mRecordList.size();
            filterResults.values = null;
        } else {
            // 否则，根据搜索关键字过滤备忘录记录
            List<Record> filteredList = new ArrayList<>();
            for (Record record : mRecordList) {
                if (record.getTitle().contains(charSequence) || record.getCon().contains(charSequence)) {
                    // 标题或内容包含搜索关键字，则添加到过滤后的列表中
                    filteredList.add(record);
                }
            }
            filterResults.count = filteredList.size();
            filterResults.values = filteredList;
        }
        return filterResults;
    }



    @Override
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {

        // 将过滤后的备忘录记录列表传递给备忘录记录适配器
        if (filterResults.count > 0) {
            List<Record> filteredList = (List<Record>) filterResults.values;
            adapter.setRecordList(filteredList);
            adapter.notifyDataSetChanged();
        }
        // 更新备忘录记录列表展示

    }

}
